package gem.model.test;

import java.awt.Component;

import gem.model.course.Course;
import gem.model.course.Department;
import gem.model.course.ScienceDomain;

import javax.swing.JTree;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public class CourseTreeModel implements TreeModel
{
	// Todo Treba promeniti modelu da se mozda ipak koriste vector-i ili
	// ArrayListe
	
	public java.util.Vector< Department > deps;
	public CourseTreeModel( java.util.Vector< Department > deps )
	{
		this.deps = deps;
	}
	
	
	@Override
	public void addTreeModelListener(TreeModelListener arg0)
	{

	}

	@Override
	public Object getChild(Object parent, int index)
	{
		if( parent instanceof Department)
		{
			Department d = (Department)parent;
			
			return d.getScienceDomains().get(index);
		}
		if( parent instanceof ScienceDomain )
		{
			ScienceDomain sd = (ScienceDomain)parent;
			
			return sd.getCourses().get(index);
		}
		if( parent instanceof String )
		{
			return deps.get(index);
		}

		return null;
	}

	@Override
	public int getChildCount(Object parent)
	{
		if( Department.class.isInstance(parent))
		{
			Department d = (Department)parent;
			return ( d.getScienceDomains() != null ? d.getScienceDomains().size() : 0 );
		}
		else if( ScienceDomain.class.isInstance(parent))
		{
			ScienceDomain sd = (ScienceDomain)parent;
			return ( sd.getCourses() != null ? sd.getCourses().size() : 0 );
		}
		else if( String.class.isInstance(parent))
		{
			//System.out.println("Prc count Strin!!");
			return deps.size();
		}
		else
		{
			System.out.println("E sad stvarno prc");
			return 0;
		}
	}

	@Override
	public int getIndexOfChild(Object parent, Object child)
	{
		// TODO Auto-generated method stub
		if( parent instanceof Department )
		{
			Department d = (Department)parent;
			
			for( ScienceDomain sd : d.getScienceDomains() )
			{
				if( sd.equals((ScienceDomain)child) )
				{
					return d.getScienceDomains().indexOf(sd);
				}
			}
			return -1;
		}
		else if( parent instanceof ScienceDomain )
		{
			ScienceDomain sd = (ScienceDomain)parent;
			
			for( Course c : sd.getCourses() )
			{
				if( c.equals((Course)child))
				{
					return sd.getCourses().indexOf(child);
				}
			}
		}
		else if( parent instanceof String )
		{
			return deps.indexOf(child);
		}
		System.out.println("Prc GetIndexofchild!!");
		return -1;
	}

	@Override
	public Object getRoot()
	{
		// TODO Auto-generated method stub
		return "Katedre";
	}

	@Override
	public boolean isLeaf( Object node )
	{
		// TODO Auto-generated method stub
		return( Course.class.isInstance(node));		
	}

	@Override
	public void removeTreeModelListener(TreeModelListener arg0)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void valueForPathChanged(TreePath arg0, Object arg1)
	{
		// TODO Auto-generated method stub

	}
	
	/**
	   * A TreeCellRenderer displays each node of a tree. The default renderer
	   * displays arbitrary Object nodes by calling their toString() method. The
	   * Component.toString() method returns long strings with extraneous
	   * information. Therefore, we use this "wrapper" implementation of
	   * TreeCellRenderer to convert nodes from Component objects to useful String
	   * values before passing those String values on to the default renderer.
	   */
	static class ItemRenderer implements TreeCellRenderer
	{
		TreeCellRenderer renderer; // ovo wrappujemo
		
		public ItemRenderer( TreeCellRenderer r )
		{
			this.renderer = r;
		}
		@Override
		public Component getTreeCellRendererComponent(JTree tree, Object value,
				boolean selected, boolean expanded, boolean leaf, int row,
				boolean hasFocus)
		{
			String newvalue = value.getClass().getName();
			return renderer.getTreeCellRendererComponent(tree, newvalue,
			          selected, expanded, leaf, row, hasFocus);
		}
	}
	
	protected static class TreeListener implements TreeSelectionListener
	{
		@Override
		public void valueChanged(TreeSelectionEvent e)
		{
			// TODO Auto-generated method stub
			
		}
	}

}
